<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\NewsTopic as NewsTopicModel;

/**
 * 资讯论坛控制器
 * Class NewsTopic
 * @package app\home\controller
 */
class NewsTopic extends Base
{
    /**
     * [getIndex  评论列表]
     * @param
     * @return json
     * @author [C。]
     */
    public function getIndex()
    {
        $model = new NewsTopicModel();
        $param = input('param.');
        $this->getPageAndSize($param);
        $map = [];
        try{
            $map['t.states'] = 0;
            $map['t.parid'] = 0;
            $map['t.newid'] = $param['newid'];
            $count = $model->getCount($map,0);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map,$this->from, $this->size,'t.id,am.id memid,am.img,am.username,am.introduction,t.content,t.imgs,t.create_time,am.identity,t.parid',0);
            foreach($lists as &$v){
                $http = substr($v,0,4);
                if($http != 'http') {
                    $v['img'] = config('domain_name') .'/'. $v['img'];
                }
                $v['addtime'] = MessageTime(strtotime($v['create_time']));
                $list = $model->getAlllist(['t.states'=>0,'t.parid'=>$v['id']]);
                $v['content'] = urldecode($v['content']);
                foreach ($list as &$v1){
                    $http = substr($v1,0,4);
                    if($http != 'http') {
                        $v1['img'] = config('domain_name') .'/'. $v1['img'];
                    }
                    $v1['addtime'] = MessageTime(strtotime($v1['create_time']));
                    $v1['content'] = urldecode($v1['content']);
                }
                $v['list'] = $list;
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [postDynamic  发布动态]
     * @param
     * @return json
     * @author [C。]
     */
    public function postAdd()
    {
        $param = input('param.');
        $model = new NewsTopicModel();
        $member = $this->user;
        try{
            $content = ping($param['content']);
            $param['content'] = urlencode($content);
            if(!empty($param['parid'])){
                $param['comment_uid'] = $member['id'];
            }else{
                $param['memid'] = $member['id'];
            }
            $id = $model->addData($param);
            $param['id'] = $id;
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '添加失败'.$e->getMessage());
        }
        return admin_json(1, '添加成功',$param);
    }
}